<template>
  <el-dialog
    :close-on-click-modal="true"
    :visible.sync="visible"
    class="print-dialog"
    append-to-body
    title="打印"
    width="70%"
    top="20px"
  >
    <div class="dialog-container">
      <div class="top">
        <el-radio-group 
          class="filter-item"
          v-model="flag"
        >
          <template v-for="item in flagList">
            <el-radio-button 
              :key="item.id" 
              :label="item.id"
            >
            {{item.text}}
            </el-radio-button>
          </template>
        </el-radio-group>
        <div class="top-buttons" style="display:flex;flex-direction:row;">
          <template v-if="data && data.infos.length === 1">
            <template v-if="data.infos[0].orderCondition === 1">
              <el-button
                :loading="submitLoading"
                @click="onClickCheck(2)"
                class="button"
                type="primary"
                ref="btn"
              >审方</el-button>
            </template>
            <template v-if="data.infos[0].orderCondition === 2">
              <el-button
                :loading="submitLoading"
                @click="onClickCheck(5)"
                class="button"
                type="primary"
                ref="btn"
              >调剂</el-button>
            </template>
          </template>
          <div>
            <el-button
            :loading="submitLoading"
            v-print="printConfig"
            class="button"
            type="primary"
            ref="btn"
          >打印</el-button>
          </div>
        </div>
      </div>
      <div id="print" class="bottom" v-loading="loading">
        <printDetails v-if="flag === 1" :data="data"></printDetails>
        <printSend v-if="flag === 2" :data="data"></printSend>
      </div>
    </div>
    <!-- <div slot="footer" class="dialog-footer">
      <el-button ref="btn" type="primary" v-print="printConfig">打印</el-button>
    </div> -->
  </el-dialog>
</template>

<script>
import { printOrder, orderCheckOrder, orderConfirmOrder } from "@/api/charges/prescription";
import printDetails from "./module/printDetails";
import printSend from "./module/printSend";

export default {
  name: "print",
  components: { printSend, printDetails },
  data() {
    return {
      flagList: [
        { id: 1, text: "默认打印" },
        { id: 2, text: "寄送单" }
      ],
      printConfig: {
        id: "#print",
        pageSize: null
      },
      submitLoading: false,
      visible: false,
      loading: false,
      orderId: null,
      data: null,
      flag: 1
    }
  },
  methods: {
    open(id) {
      this.visible = true;
      this.loading = true;
      this.orderId = id;
      this.flag = 1;

      this._loadData();
    },
    // 审方|调剂
    onClickCheck(type) {
      let infos = this.data.infos;
      let codeNo = infos[0].infoNo;
      switch(type) {
        case 2:
          // 审方
          this.submitLoading = true;
          orderCheckOrder({codeNo, type: 2 }).then(() => {
            this._success();
          }).catch(() => {
            this.submitLoading = false;
          });
          break;
        case 5:
          // 调剂
          this.submitLoading = true;
          orderConfirmOrder({codeNo, type: 2 }).then(() => {
            this._success();
          }).catch(() => {
            this.submitLoading = false;
          });
          break;
      }
    },
    _loadData() {
      printOrder(this.orderId).then(res => {
        this.loading = false;
        this.data = res;
        this._handleRefunds(res);
      });
    },
    // 处理退款
    _handleRefunds(data) {
      let refunds = data.prescriptionRefunds;
      let orderInfo = data.order;
      let infos = data.infos;

      if(!refunds) return;

      let infoMap = {};
      infos.forEach(item => {
        infoMap[item.infoNo] = item;
      });
      let info;

      refunds.forEach(item => {
        // 1:药物退款，2：项目退款，3：药材加工费退款，4：邮寄费用，5：其他费用
        switch(item.refundType) {
          case 1:
            // 药费
            orderInfo.drugCost -= item.refund;
            info = item.infoNo ? infoMap[item.infoNo]:null;
            if(info) {
              info.drugCost -= item.refund;
            }
            break;
          case 3:
            // 加工费
            orderInfo.machinFee -= item.refund;
            info = item.infoNo ? infoMap[item.infoNo]:null;
            if(info) {
              info.processCost -= item.refund;
            }
            break;
        }
      });
    },
    // 操作成功，刷新页面数据
    _success() {
      this.submitLoading = false;
      this._loadData();
      this.$notify({
        title: "操作成功",
        type: "success"
      });
      this.$emit("refresh");
    }
  }
}
</script>

<style lang="scss" scoped>
.print-dialog {
  .el-dialog__body {
    overflow: auto;
    color: #000;
  }
}

.dialog-container {
  .top {
    justify-content: space-between;
    padding: 10px 0;
    display: flex;

    .top-buttons {
      .button {
        margin-left: 10px;
      }
    }
  }
  .bottom {
    border-top: 1px solid #F6F6F6;
    padding-top: 10px;
  }
}
</style>